package mspbots.data.tl.prod.contactscience.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultMessageModel;
import mspbots.data.tl.prod.contactscience.mapper.ContactscienceCallblockMapper;
import mspbots.data.tl.prod.contactscience.model.ContactscienceCallblock;
import mspbots.data.tl.prod.contactscience.service.ContactscienceCallblockService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@DS("master")
public class ContactscienceCallblockServiceImpl extends AbstractBaseServiceImpl<ContactscienceCallblockMapper, ContactscienceCallblock>
        implements ContactscienceCallblockService {

    @Override
    public void sync(ResultMessageModel model) {
        if (ObjectUtils.isEmpty(model.getEntity()) || !ResultMessageModel.MessageModelAction.ADD.equals(model.getAction())) {
            log.error("contactscience.callblock.tl entry is null,or Action No Add");
            return;
        }
        JsonNode node = model.getEntity();
        ContactscienceCallblock data = ContactscienceCallblock.builder()
                .dials(node.get("dials").asInt())
                .suspects(node.get("suspects").asInt())
                .userName(node.get("userName").asText())
                .dailyGoal(node.get("dailyGoal").asInt())
                .totalTime(node.get("totalTime").asText())
                .appointments(node.get("appointments").asInt())
                .goalAchieved(node.get("goalAchieved").asInt())
                .conversations(node.get("conversations").asInt())
                .executedActions(node.get("executedActions").asInt())
                .conversationsToDials(node.get("conversationsToDials").asInt())
                .appointmentsToConversations(node.get("appointmentsToConversations").asInt())
                .summaryDay(LocalDate.parse(node.get("summaryDay").asText(), DateTimeFormatter.ISO_LOCAL_DATE))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(model.getTenantId());

        ContactscienceCallblock dbData = super.baseMapper.selectOne(Wrappers.<ContactscienceCallblock>lambdaQuery()
                .eq(ContactscienceCallblock::getTenantCode, data.getTenantCode())
                .eq(ContactscienceCallblock::getUserName, data.getUserName())
                .eq(ContactscienceCallblock::getSummaryDay, data.getSummaryDay())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)){
            super.baseMapper.insert(data);
        }else{
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
